package com.mymarkdown.app.fx.event.hander.impl;

import com.mymarkdown.app.fx.annotation.KeyWord;
import com.mymarkdown.app.fx.assemblys.FxTreeItem;
import com.mymarkdown.app.fx.enums.FxEventEnum;
import com.mymarkdown.app.fx.event.hander.KeyWordHandler;
import com.mymarkdown.app.fx.event.model.FxEvent;
import com.mymarkdown.app.interfaces.ModifyWindow;
import com.mymarkdown.app.interfaces.UserInfoService;
import com.mymarkdown.app.util.CurrentUserUtil;
import com.mymarkdown.app.util.FileUtil;
import com.mymarkdown.app.websocket.enums.MessageTypeEnum;
import com.mymarkdown.app.websocket.interfaces.WebSocketSendMessage;
import com.mymarkdown.app.websocket.message.MessagePojo;
import javafx.scene.control.TextInputDialog;
import javafx.scene.web.WebEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Objects;
import java.util.Optional;

/**
 * @author ：zenglun
 * @date ：Created in 2021/12/25 12:50 PM
 * @description：本地文件夹创建事件
 * @modified By：
 * @version: $
 */
@Slf4j
@KeyWord(FxEventEnum.LOCAL_DIR_SYSTEM_NEW_CRATE)
@Component
public class LocalDirSystemNewCreateMenuHandlerImpl implements KeyWordHandler {

    private FxTreeItem<String> treeItem;

    private File sourceFile;

    private String inputNewValue;

    private File createNewFile;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private WebSocketSendMessage webSocketSendMessage;

    @Autowired
    private ModifyWindow modifyWindow;

    @Override
    public void handler(WebEngine engine, FxEvent event) {
        if (sourceFile.exists()) {
            //如果是文件夹下的新建文件
            String filename = createNewFile.getParent() + "/" + inputNewValue;
            createNewFile = new File(filename);
            if (createNewFile.mkdirs()) {
                webSocketSendMessage.sendMessage(MessagePojo.empty().setMessageType(MessageTypeEnum.TISP_MSG.getMessageType()).setBody("创建成功!"));
            }else{
                webSocketSendMessage.sendMessage(MessagePojo.empty().setMessageType(MessageTypeEnum.TISP_MSG.getMessageType()).setBody("创建失败!"));
            }
        }
    }

    @Override
    public boolean afterHandler(WebEngine engine, FxEvent event) {
        treeItem = (FxTreeItem<String>) event.getTarget();
        if (Objects.isNull(treeItem)) {
            return false;
        }
        sourceFile = treeItem.getSourceFile();
        if (Objects.isNull(sourceFile)) {
            sourceFile = userInfoService.getWorkDir();
            if (Objects.isNull(sourceFile) || !sourceFile.exists()) {
                CurrentUserUtil.getSelf().sendMessage(MessagePojo.empty().setMessageType(MessageTypeEnum.TISP_MSG.getMessageType()).setBody("当前操作的工作目录不存在！"));
                return false;
            }
        }
        createNewFile = FileUtil.getCurrentDirFileName(sourceFile, "");
        TextInputDialog inputDialog = new TextInputDialog(createNewFile.getName());
        inputDialog.setHeaderText("");
        inputDialog.setTitle("文件名");
        inputDialog.setContentText("新的名称:");
        Optional<String> newVal = inputDialog.showAndWait();
        if (!newVal.isPresent()) {
            webSocketSendMessage.sendMessage(MessagePojo.empty().setMessageType(MessageTypeEnum.TISP_MSG.getMessageType()).setBody("文件名不能为空！"));
            return false;
        }
        inputNewValue = newVal.get();
        return true;
    }

    @Override
    public void beforeHandler(WebEngine engine, FxEvent event) {

    }
}
